package com.zhongwang.mms.module.buy.controller;

import com.alibaba.fastjson.JSON;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.module.buy.model.ReportPlanModel;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.buy.model.StockMarkModel;
import com.zhongwang.mms.module.buy.service.EditReportPlanService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 采购管理 -> 提报计划编辑
 * 查看 -> buy:planedit:view
 * 编辑、提交、拆分 -> buy:planedit:edit
 * 导入 -> buy:planexport:add
 * 删除 -> buy:planedit:del
 * 变更 -> buy:planedit:alter
 * 指定 -> buy:planedit:assign
 * 金额显示权限 -> buy:amount:view
 * 修改计划员 -> buy:planedit:change
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 14:42
 */
@SuppressWarnings("ALL")
@Slf4j
@Controller
@RequestMapping("/buy/editReportPlan")
public class EditReportPlanController extends BaseController {

    @Autowired
    private EditReportPlanService editReportPlanService;

    /**
     * 提报计划编辑页面
     *
     * @return 路径
     */
    @GetMapping
    @RequiresPermissions({"buy:planedit:view"})
    public String project(Model model) {
        model.addAttribute("categories", editReportPlanService.findTypes());
        model.addAttribute("planners", editReportPlanService.findPlannerList());
        return "buy/editReportPlan/page";
    }
    /**
     * 提报计划编辑页面 查看外购件未指定
     * @param queryModel 查询模式
     * @return 路径
     */
    @GetMapping("/{queryModel}")
    @RequiresPermissions({"buy:planedit:view"})
    public String queryPurNotSpecified(Model model,@PathVariable("queryModel") String queryModel) {
        model.addAttribute("categories", editReportPlanService.findTypes());
        model.addAttribute("planners", editReportPlanService.findPlannerList());
        switch (queryModel){
            case "queryPurNotSpecified":
                model.addAttribute("queryModel","外购件");
                break;
            case "queryAssNotSpecified":
                model.addAttribute("queryModel","外协件");
                break;
            case "queryMatNotSpecified":
                model.addAttribute("queryModel","原材料");
                break;
        }

        return "buy/editReportPlan/page";
    }

    /**
     * 列表数据
     *
     * @param reportPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"buy:planedit:view"})
    public JsonResult list(ReportPlanModel reportPlanModel) {
        startPage();
        List<ReportPlanModel> list = editReportPlanService.findReportPlanByQuery(reportPlanModel);
        return jsonResult(list);
    }
    /**  batchChangeList 获取批量修改状态列表
     * @param reportPlanModel
     * @return com.zhongwang.mms.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/30 9:48
     */
    @ResponseBody
    @PostMapping("/batchChangeList")
    @RequiresPermissions({"buy:planedit:view"})
    public JsonResult batchChangeList(ReportPlanModel reportPlanModel) {
        startPage();
        List<ReportPlanModel> list = editReportPlanService.findReportPlanBatchChangeListByQuery(reportPlanModel);
        return jsonResult(list);
    }

    /**╭──────────────────────────────────────────────────────────────────────╮
     *  │  @方法名: clearAssign                                                │
     *  │  @方法说明: 清空指定                                                  │
     *  │  @参数说明: [reportPlanModel]  rppId 提报计划主键                     │
     *  │  @返回类型说明: com.zhongwang.mms.common.JsonResult                  │
     *  │  @作者: 徐一贺                                                       │
     *  │  @编程时间: 2020/8/6 10:59                                           │
     *  │  @版本号：v1.0                                                       │
     *  ╰─────────────────────────────────────────────────────────────────────╯*/
    @ResponseBody
    @PostMapping("/clearAssign")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult clearAssign(ReportPlanModel reportPlanModel) {
        return editReportPlanService.clearAssign(reportPlanModel);
    }
    /**╭──────────────────────────────────────────────────────────────────────╮
     *  │  @方法名: clearRppId                                                │
     *  │  @方法说明: 清空指定的ID                                              │
     *  │  @参数说明: [reportPlanModel]  rppId 提报计划主键                     │
     *  │  @返回类型说明: com.zhongwang.mms.common.JsonResult                  │
     *  │  @作者: 徐一贺                                                       │
     *  │  @编程时间: 2020/8/7 16:20                                           │
     *  │  @版本号：v1.0                                                       │
     *  ╰─────────────────────────────────────────────────────────────────────╯*/
    @ResponseBody
    @PostMapping("/clearRppId")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult clearRppId(ReportPlanModel reportPlanModel) {
        return editReportPlanService.clearRppId(reportPlanModel);
    }
//    /**
//     * 获取数量
//     *
//     * @param reportPlanModel 查询条件
//     * @return list
//     */
//    @ResponseBody
//    @PostMapping("/getRppIsChangeStatusFlagCount")
//    @RequiresPermissions({"buy:planedit:view"})
//    public JsonResult getRppIsChangeStatusFlagCount(ReportPlanModel reportPlanModel) {
//        return editReportPlanService.getRppIsChangeStatusFlagCount(reportPlanModel);
//    }

    /**
     * 检查项目号是否存在
     *
     * @param code 项目号
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkProjectCode")
    @RequiresPermissions({"buy:planimport:view"})
    public ValidResult checkProjectCode(String rppProjectCode) {
        boolean exist = editReportPlanService.findProjectCode(rppProjectCode);
        return exist ? ValidResult.SUCCESS : ValidResult.FAIL;
    }

    ///**
    // * 添加提报计划
    // *
    // * @param reportPlan reportPlan
    // * @return json
    // */
    //@ResponseBody
    //@PostMapping("/create")
    //@RequiresPermissions({"buy:planexport:add"})
    //public JsonResult create(ReportPlanModel reportPlan) {
    //    return jsonResult(editReportPlanService.insertReportPlan(reportPlan));
    //}

    ///**
    // * 提报计划编辑 -> 导入
    // *
    // * @param file      导入文件
    // * @return JsonResult
    // */
    //@ResponseBody
    //@PostMapping("/import")
    //@RequiresPermissions({"buy:planexport:add"})
    //public JsonResult dailyTotalImport(@RequestParam("file") MultipartFile file) {
    //    return success(editReportPlanService.importFile(file));
    //}

    /**
     * 判断文件是否存在
     *
     * @param url 文件路径
     * @return
     */
    @ResponseBody
    @PostMapping("/exist")
    @RequiresPermissions({"buy:planedit:view"})
    public JsonResult exist(String url) {
        return jsonResult(editReportPlanService.exist(url));
    }

    /**
     * 删除提报计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions({"buy:planedit:del"})
    public JsonResult delete(Integer id, String reason) {
        return jsonResult(editReportPlanService.deleteById(id, reason));
    }

    /**
     * 查询提报计划，包含采购计划状态， 原始数量和其它拆分数量总和
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"buy:planedit:view"})
    public ReportPlanModel id(Integer id) {
        return editReportPlanService.findReportPlanById(id);
    }

    /**
     * 修改计划员
     *
     * @param reportPlan reportPlan
     * @return json
     */
    @ResponseBody
    @PostMapping("/edit/planner")
    @RequiresPermissions({"buy:planedit:change"})
    public JsonResult editPlanner(BReportPlan reportPlan) {
        return jsonResult(editReportPlanService.updatePlanner(reportPlan));
    }

    /**
     * 修改提报计划
     *
     * @param reportPlan reportPlan
     * @return json
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult updateProject(BReportPlan reportPlan) {
        return jsonResult(editReportPlanService.updateReportPlan(reportPlan));
    }

    /**
     * 提交提报计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult commit(Integer id) {
        return jsonResult(editReportPlanService.commitById(id));
    }

    /**
     * 提交提报计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/commitAll")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult commit(Integer[] rppIds) {
        return jsonResult(editReportPlanService.commitInId(rppIds));
    }
    /**
     * 批量撤销提报计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/cancels")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult cancels(Integer[] rppIds) {
        return jsonResult(editReportPlanService.cancels(rppIds));
    }

    /**
     * 撤消提报计划
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult cancel(Integer id) {
        return jsonResult(editReportPlanService.cancel(id));
    }

    /**
     * 拆分提报计划
     *
     * @param rppId 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/split")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult commit(Integer rppId, String[] desc, Double[] num, Integer[] status, String[] remark) {
        return jsonResult(editReportPlanService.splitReportPlan(rppId, num, status, remark, desc));
    }

    /**
     * 变更提报计划
     *
     * @param rppId 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/change")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult change(BReportPlan reportPlan) {
        return editReportPlanService.changeReportPlan(reportPlan);
    }

    /**
     * 获取标记库存
     *
     * @param stockMarkModel 条件
     * @return json
     */
    @ResponseBody
    @PostMapping("/mark/list")
    @RequiresPermissions({"buy:planedit:view"})
    public JsonResult getMarkList(StockMarkModel stockMarkModel) {
        startPage();
        List<StockMarkModel> list = editReportPlanService.findMarkList(stockMarkModel);
        return jsonResult(list);
    }

    /**
     * 指定标记库存
     *
     * @param code 提报计划ID
     * @return json
     */
    @ResponseBody
    @PostMapping("/mark")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult mark(Integer rppId, String idStr) {
        return jsonResult(editReportPlanService.mark(rppId, idStr));
    }

    /**
     * 指定标记库存
     *
     * @param reportPlanModel 计划
     * @return json
     */
    @ResponseBody
    @PostMapping("/assign")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult assign(ReportPlanModel reportPlanModel) {
        return jsonResult(editReportPlanService.assign(reportPlanModel));
    }

    /**
     * 提报计划关系查询
     *
     * @param reportPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/map")
    @RequiresPermissions({"buy:planedit:view"})
    public JsonResult mapList(ReportPlanModel reportPlanModel) {
        startPage();
        List<ReportPlanModel> list = editReportPlanService.findReportPlanMapByQuery(reportPlanModel);
        return jsonResult(list);
    }

    /**
     * 查询指定列表
     *
     * @param stockMarkModel 条件
     * @return json
     */
    @ResponseBody
    @PostMapping("/info/{id}")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult getMarkList(@PathVariable("id") Integer id) {
        startPage();
        List<StockMarkModel> list = editReportPlanService.findAssignInfoById(id);
        return jsonResult(list);
    }

    /**
     * 批量修改提报计划状态
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/batch/status")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult commit(Integer[] rppIds, Integer status) {
        return jsonResult(editReportPlanService.batchStatus(rppIds, status));
    }

//    /**
//     * 提报计划导出
//     *
//     * @param reportPlanModel 条件
//     * @return JsonResult
//     */
//    @ResponseBody
//    @GetMapping("/export")
//    @RequiresPermissions({"buy:planedit:edit"})
//    public void export(HttpServletResponse response, ReportPlanModel reportPlanModel) {
//        editReportPlanService.export(response, reportPlanModel);
//    }

    /**
     * 提报计划编辑 -> 暂缓功能修改数据导入
     *
     * @param file      导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/import/edit")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult updateImport(@RequestParam("file") MultipartFile file) {
        return success(editReportPlanService.importAndEdit(file));
    }
    /**  updateImportAssign 提报计划编辑 -> 指定导入
     * @param file
     * @return com.zhongwang.mms.common.JsonResult
     * @exception
     * @author 徐一贺
     * @date 2020/12/2 13:55
     */
    @ResponseBody
    @PostMapping("/import/editAssign")
    @RequiresPermissions({"buy:planedit:edit"})
    public JsonResult updateImportAssign(@RequestParam("file") MultipartFile file) {
        return success(editReportPlanService.importAndEditAssign(file));
    }
    /**
     * 外购件指定列表
     *
     * @return json
     */
    @ResponseBody
    @PostMapping("/list/outAssign")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult getOutAssignList() {
        startPage();
        List<ReportPlanModel> list = editReportPlanService.findOutAssignList();
        return jsonResult(list);
    }

    /**
     * 指定外购件
     *
     * @param rppId 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/outAssign")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult assign(Integer rppId) {
        return jsonResult(editReportPlanService.outAssign(rppId));
    }

    /**
     * 批量指定外购件
     *
     * @param rppIds 主键数组
     * @return json
     */
    @ResponseBody
    @PostMapping("/outAssign/batch")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult assign(Integer[] rppIds) {
        return jsonResult(editReportPlanService.batchOutAssign(rppIds));
    }


    /**
     * 列表数据
     *
     * @param reportPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list/buy")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult listBuy(String info) {
        startPage();
        List<ShoppingPlanModel> list = editReportPlanService.findBuyPlanByQuery(info);
        return jsonResult(list);
    }

    /**
     * 指定采购计划
     *
     * @param rppId 提报计划ID
     * @param sppId 采购计划ID
     * @return json
     */
    @ResponseBody
    @PostMapping("/assignPlan")
    @RequiresPermissions({"buy:planedit:assign"})
    public JsonResult delete(Integer rppId, Integer sppId) {
        return jsonResult(editReportPlanService.assignPlan(rppId, sppId));
    }


    /**
     * 提报计划暂缓导出
     *
     * @param reportPlanModel 条件
     * @return JsonResult
     */
    @ResponseBody
    @GetMapping("/deferralExport")
    @RequiresPermissions({"buy:planedit:edit"})
    public void deferralExport(HttpServletResponse response, ReportPlanModel reportPlanModel) {
        reportPlanModel.setRppPlanner(ShiroUtils.getUser().getName());
        editReportPlanService.deferralExport(response, reportPlanModel);
    }

    /**
     * 提报计划批量指定导出
     *
     * @param reportPlanModel 条件
     * @return JsonResult
     */
    @ResponseBody
    @GetMapping("/assignDeferralExport")
    @RequiresPermissions({"buy:planedit:edit"})
    public void assignDeferralExport(HttpServletResponse response, ReportPlanModel reportPlanModel) {
        reportPlanModel.setRppPlanner(ShiroUtils.getUser().getName());
        editReportPlanService.assignDeferralExport(response, reportPlanModel);
    }

    /**
     * 提报计划暂缓错误日志导出
     *
     * @param reportPlanModel 条件
     * @return JsonResult
     */
    @ResponseBody
    @GetMapping("/deferralErrorLogExport")
    @RequiresPermissions({"buy:planedit:edit"})
    public void deferralErrorLogExport(HttpServletResponse response, ReportPlanModel reportPlanModel) {
        editReportPlanService.deferralErrorLogExport(response,reportPlanModel);
    }
}
